package com.yang.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.demo.common.QueryPageParam;
import com.yang.demo.common.Result;
import com.yang.demo.entity.User;
import com.yang.demo.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/study")
public class StudyController {

    @Resource
    private UserService userService;

    @GetMapping("/get/{id}")
    public User getMsg(@PathVariable("id") Integer id) {
        return userService.getUser(id);
    }

    //分页查询用户表
    @PostMapping("/listPage")
//    public List<User> listPage(@RequestBody HashMap map){
    public List<User> listPage(@RequestBody QueryPageParam query){

//        System.out.println(query);
//        System.out.println("num=="+query.getPageNum());
//        System.out.println("size=="+query.getPageSize());
        //获取前端手动传入内容
        HashMap<String, Object> param = query.getParam();

//        System.out.println("name=="+(String)param.get("username"));
        String username = (String)param.get("username");//这里定义username是为了和User的username相匹配
//        System.out.println("id=="+(String)param.get("id"));
        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());//设置当前页数
        page.setSize(query.getPageSize());//设置每页多少条
        //模糊查找
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(User::getUsername,username);//模糊查询

        //分页
        IPage result = userService.page(page,lambdaQueryWrapper);
        System.out.println("total=="+result.getTotal());//输出符合条件的记录的条数
        return result.getRecords();
    }



    @PostMapping("/listPage1")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result listPage1(@RequestBody QueryPageParam query){
        //获取前端手动传入内容
        HashMap<String, Object> param = query.getParam();
        String username = (String)param.get("username");//这里定义username是为了和User的username相匹配
        String roleId = (String) param.get("roleId");//这里定义username是为了和User的username相匹配
        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());//设置当前页数
        page.setSize(query.getPageSize());//设置每页多少条
        //模糊查找
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(username) && !"null".equals(username)){
            lambdaQueryWrapper.like(User::getUsername,username);//模糊查询
        }
        if(StringUtils.isNotBlank(roleId) && !"null".equals(roleId)){
            lambdaQueryWrapper.eq(User::getRoleId,roleId);//模糊查询
        }
        //分页
        IPage result = userService.page(page,lambdaQueryWrapper);
        System.out.println("total=="+result.getTotal());//输出符合条件的记录的条数
        return Result.suc(result.getRecords(),result.getTotal());
    }
}
